package com.qf.controller;

import com.qf.service.CacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Set;

/**
 * @author lenovo
 * @version V1.0
 * @Date 2021/11/19 14:21
 */
@RestController
@RequestMapping("/cache")
public class CacheController {

    @Autowired
    private CacheService cacheService;

    // 存储key-value结构,value类型为String,并且设置生存时间
    @RequestMapping(value = "/save2cache-object", method = RequestMethod.POST)
    public boolean saveObject(@RequestParam(value = "key") String key, @RequestParam(value = "value") Object value){
        boolean res = cacheService.saveObject(key, value);
        return res;
    };

    @RequestMapping(value = "/get/{key}", method = RequestMethod.GET)
    public String get(@PathVariable("key") String key){

        String res = cacheService.get(key);
        return res;
    };

    @RequestMapping(value = "/get-object/{key}", method = RequestMethod.GET)
    public Object getObject(@PathVariable("key") String key){
        Object object = cacheService.getObject(key);
        return object;
    };

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public boolean del(@RequestParam("keys") String... keys){
        boolean del = cacheService.del(keys);
        return del;
    };

    @RequestMapping(value = "/hmset/{key}", method = RequestMethod.POST)
    public boolean hMSet(@PathVariable("key") String key, @RequestBody Map map){
        boolean res = cacheService.hMSet(key, map);
        return res;
    };

    @RequestMapping(value = "/hmget/{key}", method = RequestMethod.GET)
    public Map hMGet(@PathVariable("key") String key){
        Map map = cacheService.hMGet(key);
        return map;
    };

    @RequestMapping(value = "/save2cache", method = RequestMethod.POST)
    public boolean set(
            @RequestParam("key") String key,
            @RequestParam("value") String value,
            @RequestParam("expireSecond") Long expireSecond){
        boolean res = cacheService.set(key, value, expireSecond);

        return res;
    };

    @RequestMapping(value = "/save2cache-persist", method = RequestMethod.POST)
    public boolean saveCache(@RequestParam("key") String key, @RequestParam("value") String value){

        boolean res = cacheService.saveCache(key, value);
        return res;
    };

    @RequestMapping(value = "/incr", method = RequestMethod.GET)
    public Long incr(@RequestParam("key") String key, @RequestParam("delta") long delta){

        Long incr = cacheService.incr(key, delta);
        return incr;
    };

    @RequestMapping(value = "/sadd")
    public boolean sadd(@RequestParam("key") String key, @RequestParam("value") String... value){

        boolean res = cacheService.sadd(key, value);
        return res;
    };

    @RequestMapping(value = "/hget/{key}/{field}",method = RequestMethod.POST)
    public Object hget(@PathVariable(value = "key") String key,
                       @PathVariable(value = "field") String field){
        Object object = cacheService.hget(key, field);
        return object;
    };

    @RequestMapping(value = "/hincr", method = RequestMethod.GET)
    public Long hincr(@RequestParam("key") String key, @RequestParam("field") String field,@RequestParam("delta") long delta){

        Long res = cacheService.hincr(key, field, delta);
        return res;
    };

    @RequestMapping(value = "/seget/{key}",method = RequestMethod.POST)
    public Set<String> setGet(@PathVariable(value = "key")String key){

        Set<String> res = cacheService.setGet(key);

        return res;
    }

    @RequestMapping(value = "/seSet",method = RequestMethod.GET)
    public Boolean setSet(@RequestParam(value = "key")String key,@RequestParam("value") Set<String> value){

        Boolean res = cacheService.setSet(key,value);
        return res;
    }
    @RequestMapping(value = "/interSet",method = RequestMethod.GET)
    public Set<String> interSet(@RequestParam(value = "key") Set<String> key) {
        Set<String> set = cacheService.interSet(key);

        return set;
    }

}
